/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utils;

import java.util.Iterator;
import templates.PropertyTemplate;
import templates.Template;

/**
 *
 * @author KAV
 *
 */
public class QueryBuilder {

    public String constractSelectQuery(Template template, Short uidForFind, boolean isQueryByPrimaryKey) {
        StringBuilder stWhere = new StringBuilder();
        StringBuilder stSelect = new StringBuilder();
        stSelect.append("SELECT ");
        Iterator<PropertyTemplate> it = template.getPropertyTemplateCollection().iterator();
        while (it.hasNext()) {
            PropertyTemplate pt = it.next();
            stSelect.append(pt.getName());
            stSelect.append((it.hasNext()) ? ", " : " ");
            if (isQueryByPrimaryKey) {
                if ((uidForFind == null) && (pt.isPrimaryKey())) {
                    stWhere.append(" WHERE ").append(pt.getName()).append("=?");
                }
            } else if ((uidForFind != null) && (pt.getUid() == (uidForFind.shortValue()))) {
                if ((pt.getType().equals("DatabaseNumber"))||(pt.getType().equals("DatabaseFloat"))) {
                    stWhere.append(" WHERE ").append(pt.getName()).append("=?");
                }else if ((pt.getType().equals("DatabaseText"))||(pt.getType().equals("DatabasePhone"))||(pt.getType().equals("DatabaseEmail"))) {
                    stWhere.append(" WHERE REGEXP_LIKE(").append(pt.getName()).append(",?,'i')");
                }else if (pt.getType().equals("DatabaseDate")) {
                    stWhere.append(" WHERE ").append(pt.getName()).append("=?");
                }
            }
        }
        stSelect.append("FROM ").append(template.getTableName());
        return stSelect.toString() + stWhere.toString();
    }

    public String constractInsertQuery(Template template) {
        StringBuilder stSelect = new StringBuilder();
        stSelect.append("INSERT INTO ").append(template.getTableName()).append("(");
        Iterator<PropertyTemplate> it = template.getPropertyTemplateCollection().iterator();
        while (it.hasNext()) {
            PropertyTemplate pt = it.next();
            if (!pt.isPrimaryKey()) {
                stSelect.append(pt.getName());
                stSelect.append((it.hasNext()) ? ", " : ")");
            }
        }
        stSelect.append("VALUES (");
        it = template.getPropertyTemplateCollection().iterator();
        while (it.hasNext()) {
            PropertyTemplate pt = it.next();
            if (!pt.isPrimaryKey()) {
                if (pt.getType().equals("DatabaseDate")) {
                    stSelect.append("TO_DATE(?, 'DD.MM.YYYY')");
                } else {
                    stSelect.append("?");
                }
                stSelect.append((it.hasNext()) ? ", " : ")");
            }
        }
        return stSelect.toString();
    }

    public String constractUpdateQuery(Template template) {
        StringBuilder stSelect = new StringBuilder();
        stSelect.append("UPDATE ").append(template.getTableName()).append(" SET ");
        Iterator<PropertyTemplate> it = template.getPropertyTemplateCollection().iterator();
        while (it.hasNext()) {
            PropertyTemplate pt = it.next();
            if (!pt.isPrimaryKey()) {
                if (pt.getType().equals("DatabaseDate")) {
                    stSelect.append(pt.getName()).append("=TO_DATE(?, 'DD.MM.YYYY')");
                } else {
                    stSelect.append(pt.getName()).append("=?");
                }
                stSelect.append((it.hasNext()) ? ", " : " ");
            }
        }
        stSelect.append("WHERE ");
        it = template.getPropertyTemplateCollection().iterator();
        while (it.hasNext()) {
            PropertyTemplate pt = it.next();
            if (pt.isPrimaryKey()) {
                stSelect.append(pt.getName()).append("=?");
                break;
            }
        }
        return stSelect.toString();
    }

    public String constractDeleteQuery(Template template) {
        StringBuilder stSelect = new StringBuilder();
        stSelect.append("DELETE FROM ").append(template.getTableName()).append(" WHERE ");
        Iterator<PropertyTemplate> it = template.getPropertyTemplateCollection().iterator();
        while (it.hasNext()) {
            PropertyTemplate pt = it.next();
            if (pt.isPrimaryKey()) {
                stSelect.append(pt.getName()).append("=?");
                break;
            }
        }
        return stSelect.toString();
    }
}
